<template>
  <div class="login-account">
    <el-form
      ref="formRef"
      label-width="60px"
      label-position="top"
      :rules="rules"
      :model="phoneCode"
    >
      <el-form-item prop="phone">
        <template #label>Phone</template>
        <el-input
          placeholder="Phone Number"
          v-model="phoneCode.phone"
        ></el-input>
      </el-form-item>

      <el-form-item prop="code" class="el-form-phone">
        <template #label> Verification code </template>
        <div class="get-code">
          <el-input placeholder="Verification code" v-model="phoneCode.code" />
          <el-button
            type="primary"
            class="get-code"
            @click="handleGetCode"
            :disabled="isDisable"
          >
            {{ showMessage }}
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'

import { rules } from '../config/phone-config'

import { ElForm } from 'element-plus'
import 'element-plus/es/components/form/style/css'

const formRef = ref<InstanceType<typeof ElForm>>()

const phoneCode = reactive({
  phone: '',
  code: '',
})

const showMessage = ref('Send')
const isDisable = ref(false)

// 获取验证码
const handleGetCode = () => {
  formRef.value?.validateField('phone', (valid) => {
    // 1.判断手机号是否正确
    if (!valid) {
      return
    }

    // 2. 发送获取验证码请求

    // 3. 修改 Send 按钮
    isDisable.value = true
    let wait = 10
    showMessage.value = `Resend ( ${wait} )`
    const timer = setInterval(() => {
      console.log(1)
      wait--
      showMessage.value = `Resend ( ${wait} )`
      if (wait <= 0) {
        showMessage.value = 'Send'
        isDisable.value = false
        clearInterval(timer)
      }
    }, 1000)
  })
}

const loginAction = () => {
  // 1.判断用户信息
  formRef.value?.validate((valid: boolean) => {
    if (!valid) {
      return
    }
    //  2.判断验证码
  })
}

defineExpose({
  loginAction,
})
</script>

<style scoped lang="less">
.el-form-phone {
  margin-top: 30px;
}
.get-code {
  width: 100%;
  display: flex;

  .get-code {
    width: 139px;
    height: 55px;
    background: #3772ff;
    border-radius: 10px;
    margin-left: 9px;
    border: none;
  }
}
:deep(.el-input__inner) {
  height: 55px;
  border-radius: 10px;
  padding-left: 17px !important;
}
:deep(.el-form-item__label) {
  font-size: 18px;
  font-family: SF Pro Display;
  font-weight: 400;
  color: #707070;
}
:deep(.el-button.is-disabled) {
  background-color: #999;
}
</style>
